//
//  VoiceSecondViewController.m
//  SmileRead
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "VoiceSecondViewController.h"
#import "FileNameList.h"
#import "VoiceModel.h"
#import "AsyncPOSTParser.h"
#import "VoiceMusicModel.h"
#import "VoiceMusicCell.h"
#import "VoiceDetailViewController.h"
#import "MJRefresh.h"
#import "Path.h"
#import "AppDelegate.h"


@interface VoiceSecondViewController ()<UITableViewDataSource, UITableViewDelegate>

{
    NSInteger _start;
    NSInteger _i;
}

@property (nonatomic, retain) NSMutableArray *arrayOflist;

@property (nonatomic, retain) NSDictionary *dicOfMusic;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSString *radioID;

@property (nonatomic, retain) VoiceModel *model;


@end

@implementation VoiceSecondViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrayOflist = [NSMutableArray array];
        self.dicOfMusic = [NSDictionary dictionary];
    }
    return self;
}

- (void)dealloc
{
    [_model release];
    [_radioID release];
    [_tableView release];
    [_dicOfMusic release];
    [_arrayOflist release];
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated
{
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)receiveWithModel:(VoiceModel *)model
{
    // 解析数据 radioid=545b4e758ead0e4a06000041&start=0&client=2&limit=10
    self.radioID = model.radioid;
    self.model = model;
    NSDictionary *dic = @{@"radioid" : self.radioID, @"start" : @"0", @"lient" : @"2", @"limit" : @"10"};
    [AsyncPOSTParser asyncPOSTParserWithURLString:LY_VoiceOfMusic fileName:FILE_LY_VoiceOfMusic pamarters:dic block:^(NSData *data) {
       
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dicOfMusic = [[dic objectForKey:@"data"] objectForKey:@"radioInfo"];
        
        NSArray *array = [[dic objectForKey:@"data"] objectForKey:@"list"];
        for (int i = 0; i < array.count; i++) {
            VoiceMusicModel *model = [[VoiceMusicModel alloc] init];
            [model setValuesForKeysWithDictionary:array[i]];
            [self.arrayOflist addObject:model];
        }
        
        //[self.mb removeFromSuperview];
        
        [self createTableView];
        [self createHeaderView];
    }];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor grayColor];
    [self.view addSubview:self.tableView];
    [self refresh];
    [_tableView release];
}

- (void)createHeaderView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300 * heightFit)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250 * heightFit)];
    [Path pathWithImageView:imageView URLString:self.model.coverimg];
    UIView *sview = [[UIView alloc] initWithFrame:CGRectMake(0, 300 * heightFit, self.view.frame.size.width, 0.5 * heightFit)];
    sview.backgroundColor = [UIColor grayColor];
    [headerView addSubview:sview];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10 * widthFit, 250 * heightFit, self.view.frame.size.width - 20 * widthFit, 50 * heightFit)];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:17];
    //label.textColor = [UIColor blueColor];
    NSString *string = [NSString stringWithFormat:@"🎵 %@", self.model.desc];
    label.text = string;
//    label.textColor = [Path colorWithRed:242 green:216 blue:56];
    [headerView addSubview:label];
    [headerView addSubview:imageView];
    self.tableView.tableHeaderView = headerView;

    [sview release];
    [imageView release];
    [label release];
}

- (void)refresh
{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _i = 0;
        // 线删除原有数据
        NSString *path = [Path pathOfLibraryWithFileName:FILE_LY_VoiceOfMusic];
        NSFileManager *file = [NSFileManager defaultManager];
        if ([file isExecutableFileAtPath:path]) {
            [file removeItemAtPath:path error:nil];
        }
        [self.arrayOflist removeAllObjects];
        
        // 开始解析
        
        NSDictionary *dic = @{@"radioid" : self.radioID, @"start" : @"0", @"lient" : @"2", @"limit" : @"10"};
        [AsyncPOSTParser asyncPOSTParserWithURLString:LY_VoiceOfMusic fileName:FILE_LY_VoiceOfMusic pamarters:dic block:^(NSData *data) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            self.dicOfMusic = [[dic objectForKey:@"data"] objectForKey:@"radioInfo"];
            
            NSArray *array = [[dic objectForKey:@"data"] objectForKey:@"list"];
            for (int i = 0; i < array.count; i++) {
                VoiceMusicModel *model = [[VoiceMusicModel alloc] init];
                [model setValuesForKeysWithDictionary:array[i]];
                [self.arrayOflist addObject:model];
            }
            [self.tableView reloadData];
            [self.tableView.header endRefreshing];
        }];
    }];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //radioid=55a481fa53479c45528b4607&start=10&client=2&limit=10
        _start = (_i + 1) * 10;
        _i++;
        NSString *start = [NSString stringWithFormat:@"%ld", _start];
        NSDictionary *dic = @{@"radioid" : self.radioID, @"start" : start, @"client" : @"2", @"limit" : @"10"};
        NSString *urlStr = [NSString stringWithFormat:@"%@_list", LY_VoiceOfMusic];
        
        [AsyncPOSTParser asyncPOSTParserWithURLString:urlStr fileName:FILE_LY_VoiceOfMusic pamarters:dic block:^(NSData *data) {
           
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            self.dicOfMusic = [[dic objectForKey:@"data"] objectForKey:@"radioInfo"];
            
            NSArray *array = [[dic objectForKey:@"data"] objectForKey:@"list"];
            for (int i = 0; i < array.count; i++) {
                VoiceMusicModel *model = [[VoiceMusicModel alloc] init];
                [model setValuesForKeysWithDictionary:array[i]];
                [self.arrayOflist addObject:model];
            }
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        }];
        
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayOflist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"cell";
    VoiceMusicCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    if (cell == nil) {
        cell = [[[VoiceMusicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident] autorelease];
    }
    if (self.arrayOflist.count > 0) {
        VoiceMusicModel *model = self.arrayOflist[indexPath.row];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * heightFit;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VoiceDetailViewController *detail = [[VoiceDetailViewController alloc] init];
    
    [detail receiveWithDic:self.dicOfMusic];
    [detail receiveWithModel:self.arrayOflist[indexPath.row]];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.window.rootViewController presentViewController:detail animated:YES completion:^{
        
    }];
    
    [detail release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}


@end
